package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.database.dao.Account;
import com.database.dao.AccountDao;
import com.database.dao.DaoSession;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;
import de.greenrobot.dao.query.WhereCondition;

public class MainActivity extends AppCompatActivity {
    private DaoSession daoSession;
    private AccountDao accountDao;
    private Button button;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.textView);
        daoSession = MyApplication.getDaoSession(this);
        accountDao = daoSession.getAccountDao();
    }

    public void on(View v) {
        Account account = new Account(null, 0d, 0d, "备注", 0d, new Date());
        Long myid = accountDao.insert(account);
        Log.i("insertID", "" + myid);
        accountDao.deleteByKey(myid);
        account.setMonthUsing(123d);
        accountDao.update(account);
        Query<Account> query =  accountDao.queryRawCreate("where _id >= ?","21");
        int list = query.list().size();
//        List list = accountDao.queryRaw("where _id >= ?" , "21");
//        if (list != null)
//            textView.setText("notnull");
//        textView.setText("null");
        textView.setText(list+"");

        QueryBuilder builder = accountDao.queryBuilder();
        WhereCondition equal = AccountDao.Properties.Id.eq(21);//等于
        WhereCondition not_equal = AccountDao.Properties.Id.notEq(21);//不等于
        WhereCondition less_thanl = AccountDao.Properties.Id.lt(21);//小于
        WhereCondition greater_than = AccountDao.Properties.Id.gt(21);//大于
        WhereCondition less_or_equal = AccountDao.Properties.Id.le(21);//小于等于
        WhereCondition greater_or_equal = AccountDao.Properties.Id.ge(21);//大于等于
        WhereCondition between_and = AccountDao.Properties.Id.between("21","31");//在21和31之间
        Set<Integer> set = new HashSet<Integer>();
        set.add(21);
        set.add(31);
        set.add(11);
        WhereCondition in  = AccountDao.Properties.Id.in(set);//id在集合之中,即id是否存在11，21，31之中
        WhereCondition like  = AccountDao.Properties.Id.like("%dao%");//模糊查找含有dao的字符
        Query query1 =  builder.where(greater_than).build();

        list = query1.list().size();
        textView.append(""+list);

    }


}
